package org.link.ui;

import java.awt.Color;
import java.awt.EventQueue;
import java.awt.Font;
import java.awt.GridLayout;
import java.awt.Insets;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.Random;

import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JMenu;
import javax.swing.JMenuBar;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.SwingConstants;
import javax.swing.border.EmptyBorder;

import org.link.bean.Paper;
import org.link.dao.MysqlDao;
import org.link.util.ManagerAllData;
import org.link.util.UserAllData;
import org.link.util.Util;

/**
 * 
 * project_name:jksystem
 * package_name:org.link.ui
 * file_name:mainFrame.java
 * @author Link
 * create_date:2015年9月19日下午4:06:26
 * modified_date:
 * description: 主面板类(考试系统主界面)
 *
 */
public class MainFrame extends JFrame {

	private JPanel mainPane;				//主面板
	private JMenuBar menuBar;				//菜单栏
	private JButton countButton;			//错题统计按钮
	private JButton chapterButton;			//章节练习按钮
	private JButton orderButton;			//顺序练习按钮
	private JButton examButton;				//模拟考试按钮
	private JButton recordeButton;			//考试记录按钮
	private JMenu jMenu;					//菜单项
	private JButton returnButton;			//返回上层按钮
	private JButton firstChapterButton;		//第一章按钮
	private JButton secondChapterButton;	//第二章按钮
	private JButton threeChapterButton;		//第三章按钮
	private JButton fourChapterButton;		//第四章按钮
	private MysqlDao mysqlDao = new MysqlDao();
	private JLabel userLabel;				//学员专区标签
	private JLabel managerLabel;			//管理员专区标签
	private JButton paperButton;			//生成试卷按钮
	private JButton inputQuestionButton;	//录入试题按钮
	private JPanel questionPanel;			//题目面板
	private JButton questionButton;			//题号按钮
	private	JButton paperSureButton;		//试卷生成按钮
	private JButton resetButton;			//重置按钮
	private JLabel promptCountLabel;		//选题数目提示标签
	private JLabel promptLabel;				//选题提示标签
	private int num = 1;					//试题编号
	private int id = 0;						//试卷id号
	private QuesBtnActionListener qbal = new QuesBtnActionListener();
	private String userName;

	/**
	 * Launch the application.
	 */
	public static void main(String[] args) {
		EventQueue.invokeLater(new Runnable() {
			public void run() {
				try {
					MainFrame frame = new MainFrame("link");
					frame.setVisible(true);
				} catch (Exception e) {
					e.printStackTrace();
				}
			}
		});
	}

	/**
	 * Create the frame.
	 */
	public MainFrame(String userName) {
		this.userName = userName;
		setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		setBounds(200, 150, 699, 479);
		setResizable(false);
		Util.screen(this);
		setTitle("驾考系统_管理员");
		mainPane = new JPanel();
		mainPane.setBackground(Color.WHITE);
		mainPane.setBorder(new EmptyBorder(5, 5, 5, 5));
		setContentPane(mainPane);
		mainPane.setLayout(null);
		
		/**
		 * 返回上层按钮
		 */
		returnButton = new JButton("返回上层");
		returnButton.setFont(new Font("微软雅黑", Font.PLAIN, 16));
		returnButton.setBounds(20, 10, 102, 37);
		returnButton.addActionListener(new ActionListener() {
			@Override
			public void actionPerformed(ActionEvent e) {
				mainPane.removeAll();
				mainPane.repaint();
				jMenu.setText("科目一");
				mainPane.add(chapterButton);
				mainPane.add(countButton);
				mainPane.add(examButton);
				mainPane.add(orderButton);
				mainPane.add(recordeButton);
				mainPane.add(managerLabel);
				mainPane.add(paperButton);
				mainPane.add(userLabel);
				mainPane.add(inputQuestionButton);
			}
		});
		
		/**
		 * 第一章按钮
		 */
		firstChapterButton = new JButton("第一章: 道路交通安全法律,法规规章(10题)");
		firstChapterButton.setHorizontalAlignment(SwingConstants.LEADING);
		firstChapterButton.setFont(new Font("微软雅黑", Font.PLAIN, 14));
		firstChapterButton.setBounds(100, 74, 410, 33);
		firstChapterButton.addActionListener(new ActionListener() {
			@Override
			public void actionPerformed(ActionEvent e) {
				if(UserAllData.chapters.size() == 0){
					mysqlDao.readQuestionTabel(UserAllData.chapters);
					new ChapterFrame(0,9).setVisible(true);
				}else{
					new ChapterFrame(0,9).setVisible(true);
				}
			}
		});
		/**
		 * 第二章按钮
		 */
		secondChapterButton = new JButton("第二章: 交通信号(10题)");
		secondChapterButton.setHorizontalAlignment(SwingConstants.LEADING);
		secondChapterButton.setFont(new Font("微软雅黑", Font.PLAIN, 14));
		secondChapterButton.setBounds(100, 137, 410, 33);
		secondChapterButton.addActionListener(new ActionListener() {
			@Override
			public void actionPerformed(ActionEvent e) {
				if(UserAllData.chapters.size() == 0){
					mysqlDao.readQuestionTabel(UserAllData.chapters);
					new ChapterFrame(10,19).setVisible(true);
				}else{
					new ChapterFrame(10,19).setVisible(true);
				}
				
			}
		});
		
		/**
		 * 第三章按钮
		 */
		threeChapterButton = new JButton("第三章: 行车安全,文明驾驶基础知识(10题)");
		threeChapterButton.setHorizontalAlignment(SwingConstants.LEADING);
		threeChapterButton.setFont(new Font("微软雅黑", Font.PLAIN, 14));
		threeChapterButton.setBounds(100, 203, 410, 33);
		threeChapterButton.addActionListener(new ActionListener() {
			@Override
			public void actionPerformed(ActionEvent e) {
				if(UserAllData.chapters.size() == 0){
					mysqlDao.readQuestionTabel(UserAllData.chapters);
					new ChapterFrame(20,29).setVisible(true);
				}else{
					new ChapterFrame(20,29).setVisible(true);
				}
				
			}
		});
		
		/**
		 * 第四章按钮
		 */
		fourChapterButton = new JButton("第四章: 机动车驾驶操作相关基础知识(10题)");
		fourChapterButton.setHorizontalAlignment(SwingConstants.LEADING);
		fourChapterButton.setFont(new Font("微软雅黑", Font.PLAIN, 14));
		fourChapterButton.setBounds(100, 270, 410, 33);
		fourChapterButton.addActionListener(new ActionListener() {
			@Override
			public void actionPerformed(ActionEvent e) {
				if(UserAllData.chapters.size() == 0){
					mysqlDao.readQuestionTabel(UserAllData.chapters);
					new ChapterFrame(30,39).setVisible(true);
				}else{
					new ChapterFrame(30,39).setVisible(true);
				}
				
			}
		});
		
		/**
		 * 章节练习按钮
		 */
		chapterButton = new JButton("章节练习");
		chapterButton.setFont(new Font("微软雅黑", Font.PLAIN, 16));
		chapterButton.setBounds(174, 90, 110, 42);
		mainPane.add(chapterButton);
		chapterButton.addActionListener(new ActionListener() {
			@Override
			public void actionPerformed(ActionEvent e) {
				if(MainFrame.this.getTitle().equals("驾考系统_学员")){
					mainPane.removeAll();
					mainPane.repaint();
					jMenu.setText("科目一(章节练习)");
					mainPane.add(returnButton);
					mainPane.add(firstChapterButton);
					mainPane.add(secondChapterButton);
					mainPane.add(threeChapterButton);
					mainPane.add(fourChapterButton);
				}
			}
		});
		
		orderButton = new JButton("随机练习");
		orderButton.setFont(new Font("微软雅黑", Font.PLAIN, 16));
		orderButton.setBounds(174, 173, 110, 42);
		mainPane.add(orderButton);
		
		/**
		 * 模拟考试按钮
		 */
		examButton = new JButton("模拟考试");
		examButton.setFont(new Font("微软雅黑", Font.PLAIN, 16));
		examButton.setBounds(326, 90, 110, 42);
		mainPane.add(examButton);
		examButton.addActionListener(new ActionListener() {
			@Override
			public void actionPerformed(ActionEvent arg0) {
				if(MainFrame.this.getTitle().equals("驾考系统_学员")){
					UserAllData.userExamPapers.clear();
					int num = mysqlDao.queryPaperMaxId();
					int id = new Random().nextInt(num)+1;//生成随机数
					mysqlDao.readPaperTabel(id, MainFrame.this.userName);//执行将试卷信息加载到内存中
					new ExamFrame().setVisible(true);//加载考试界面
				}
			}
		});
		
		recordeButton = new JButton("考试记录");
		recordeButton.setFont(new Font("微软雅黑", Font.PLAIN, 16));
		recordeButton.setBounds(326, 173, 110, 42);
		mainPane.add(recordeButton);
		
		countButton = new JButton("错题统计");
		countButton.setFont(new Font("微软雅黑", Font.PLAIN, 16));
		countButton.setBounds(482, 90, 110, 42);
		mainPane.add(countButton);
		
		userLabel = new JLabel("学员专区: ");
		userLabel.setForeground(Color.BLUE);
		userLabel.setFont(new Font("微软雅黑", Font.BOLD, 18));
		userLabel.setBounds(39, 135, 85, 42);
		mainPane.add(userLabel);
		
		managerLabel = new JLabel("管理员专区: ");
		managerLabel.setForeground(Color.BLUE);
		managerLabel.setFont(new Font("微软雅黑", Font.BOLD, 18));
		managerLabel.setBounds(39, 277, 100, 42);
		mainPane.add(managerLabel);
		
		inputQuestionButton = new JButton("数据库备份");
		inputQuestionButton.setFont(new Font("微软雅黑", Font.PLAIN, 16));
		inputQuestionButton.setBounds(326, 278, 110, 42);
		inputQuestionButton.setMargin(new Insets(0, 0, 0, 0));
		mainPane.add(inputQuestionButton);
		
		/**
		 * 生成试卷按钮
		 */
		paperButton = new JButton("生成试卷");
		paperButton.setFont(new Font("微软雅黑", Font.PLAIN, 16));
		paperButton.setBounds(174, 278, 110, 42);
		mainPane.add(paperButton);
		paperButton.addActionListener(new ActionListener() {
			@Override
			public void actionPerformed(ActionEvent arg0) {
				// TODO Auto-generated method stub
				if(MainFrame.this.getTitle().equals("驾考系统_管理员")){
					if(ManagerAllData.chapters.size()==0){
						mysqlDao.readQuestionTabel(ManagerAllData.chapters);
						MainFrame.this.id = mysqlDao.queryPaperMaxId()+1;
						mainPane.removeAll();
						mainPane.repaint();
						jMenu.setText("科目一(生成试卷)");
						mainPane.add(returnButton);
						mainPane.add(questionPanel);
						mainPane.add(promptCountLabel);
						mainPane.add(promptLabel);
						mainPane.add(paperSureButton);
						mainPane.add(resetButton);
					}else{
						MainFrame.this.id = mysqlDao.queryPaperMaxId()+1;
						mainPane.removeAll();
						mainPane.repaint();
						jMenu.setText("科目一(生成试卷)");
						mainPane.add(returnButton);
						mainPane.add(questionPanel);
						mainPane.add(promptCountLabel);
						mainPane.add(promptLabel);
						mainPane.add(paperSureButton);
						mainPane.add(resetButton);
					}
					
				}
			}
		});
		
		menuBar = new JMenuBar();
		setJMenuBar(menuBar);
		
		jMenu = new JMenu("科目一");
		jMenu.setBackground(Color.PINK);
		jMenu.setFont(new Font("微软雅黑", Font.BOLD, 15));
		menuBar.add(jMenu);
		
		/**
		 * 试题编号面板
		 */
		questionPanel = new JPanel();
		questionPanel.setBounds(20, 80, 655, 140);
		questionPanel.setLayout(new GridLayout(4, 10, 5, 5));
		for(int i = 1;i<5;i++){
			for(int j = 1;j<11;j++){
				questionButton = new JButton(i+"."+j);
				questionButton.addActionListener(qbal);
				questionPanel.add(questionButton);
			}
		}
		promptCountLabel = new JLabel("提示: 选择其中25道题");
		promptCountLabel.setBounds(340, 18, 180, 30);
		promptCountLabel.setFont(new Font("微软雅黑", Font.PLAIN, 16));
		promptCountLabel.setForeground(Color.red);
		
		promptLabel = new JLabel();
		promptLabel.setBounds(180, 249, 350, 60);
		promptLabel.setFont(new Font("微软雅黑", Font.PLAIN, 18));
		promptLabel.setForeground(Color.blue);
		
		/**
		 * 试卷生成确认按钮
		 */
		paperSureButton  = new JButton("提 交");
		paperSureButton.setBounds(480, 330, 90, 40);
		paperSureButton.setFont(new Font("微软雅黑", Font.PLAIN, 16));
		paperSureButton.addActionListener(new ActionListener() {
			@Override
			public void actionPerformed(ActionEvent arg0) {
				if(MainFrame.this.num!=26){
					JOptionPane.showMessageDialog(null, "选题不足25题,请继续选择!");
					return;
				}else{
					if(mysqlDao.addPaper() == 1){
						JOptionPane.showMessageDialog(null, "添加成功!");
						promptLabel.setText("");
						questionPanel.removeAll();
						questionPanel.repaint();
						for(int i = 1;i<5;i++){
							for(int j = 1;j<11;j++){
								questionButton = new JButton(i+"."+j);
								questionButton.addActionListener(qbal);
								questionPanel.add(questionButton);
							}
						}
						MainFrame.this.num = 1;
						ManagerAllData.papers.clear();
						MainFrame.this.id++;
					}else{
						JOptionPane.showMessageDialog(null, "添加失败!");
					}
				}
			}
		});
		
		/**
		 * 重置按钮
		 */
		resetButton = new JButton("重 置");
		resetButton.setBounds(200, 330, 90, 40);
		resetButton.setFont(new Font("微软雅黑", Font.PLAIN, 16));
		resetButton.addActionListener(new ActionListener() {
			@Override
			public void actionPerformed(ActionEvent arg0) {
				promptLabel.setText("");
				questionPanel.removeAll();
				questionPanel.repaint();
				for(int i = 1;i<5;i++){
					for(int j = 1;j<11;j++){
						questionButton = new JButton(i+"."+j);
						questionButton.addActionListener(qbal);
						questionPanel.add(questionButton);
					}
				}
				MainFrame.this.num = 1;
				ManagerAllData.papers.clear();
			}
		});
	}
	
	/**
	 * 
	 * project_name:jksystem
	 * package_name:org.link.ui
	 * file_name:MainFrame.java
	 * @author Link
	 * create_date:2015年9月20日上午11:59:50
	 * modified_date:
	 * description: 内部类,选题按钮的事件监听
	 *
	 */
	class QuesBtnActionListener implements ActionListener{

		@Override
		public void actionPerformed(ActionEvent e) {
			// TODO Auto-generated method stub
			if(num == 26){
				JOptionPane.showMessageDialog(null, "已选完25题,请点击提交或重置按钮重新选择!");
				return;
			}else{
				JButton btn = (JButton) e.getSource();
				btn.setEnabled(false);
				promptLabel.setText("已累计选择"+num+"题,灰色按钮处为选中题号");
				int id = MainFrame.this.id;
				String questionId = btn.getText();
				int tittleId = num;
				Paper paper = new Paper(id, tittleId, questionId);
				ManagerAllData.papers.add(paper);
				num++;
			}
			
		}
		
	}
}



